package com.toolsder.demo.spring.security.jwt.service.impl;

import com.toolsder.demo.spring.security.jwt.entity.RoleVO;
import com.toolsder.demo.spring.security.jwt.entity.database.Resources;
import com.toolsder.demo.spring.security.jwt.entity.database.Role;
import com.toolsder.demo.spring.security.jwt.repository.ResourcesRepository;
import com.toolsder.demo.spring.security.jwt.repository.RoleRepository;
import com.toolsder.demo.spring.security.jwt.service.RoleService;
import com.toolsder.demo.spring.security.jwt.util.SnowFlake;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.*;

/**
 * created by 猴子请来的逗比 On 2020/8/11
 *
 * @author by 猴子请来的逗比
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleRepository roleRepository;

    @Resource
    private ResourcesRepository resourcesRepository;
    /**
     * 注入雪花算法
     */
    @Resource
    private SnowFlake snowFlake;

    @Override
    public Role findByRoleCode(String code) {
        return roleRepository.findByRoleCode(code);
    }

    /**
     * 资源是否存在
     *
     * @param code
     */
    @Override
    public boolean existsByRoleCode(String code) {
        return roleRepository.existsByRoleCode(code);
    }


    /**
     * 保存资源
     *
     * @param code
     * @param name
     * @param descpt
     */
    @Override
    public void create(String code, String name, String descpt) {
        Role role = new Role();
        role.setId(snowFlake.nextId());
        role.setRoleName(name);
        role.setRoleCode(code.toUpperCase());
        role.setDescpt(descpt);
        roleRepository.save(role);

    }

    @Override
    public void updateMessage(String code, String name, String descpt) {
        Role role = this.findByRoleCode(code);
        role.setRoleName(name);
        role.setDescpt(descpt);
        roleRepository.save(role);
    }

    /**
     * 更新权限资源
     *
     * @param gjg2Resources
     */
    @Override
    public void updateResources(String code, List<Resources> gjg2Resources) {
        Role role = this.findByRoleCode(code);
        Set<Resources> resources = role.getResources();
        if (null == resources) {
            resources = new HashSet<>(gjg2Resources);
        } else {
            resources.addAll(gjg2Resources);
        }
        role.setResources(resources);
        roleRepository.save(role);
    }

    /**
     * 创建超级管理员角色
     *
     * @param code
     * @param name
     * @param descpt
     * @param resourcesList
     */
    @Override
    public void createSuperAdmin(String code, String name, String descpt, List<Resources> resourcesList) {
        Role role = new Role();
        role.setId(snowFlake.nextId());
        role.setRoleName(name);
        role.setRoleCode(code.toUpperCase());
        role.setDescpt(descpt);
        Set<Resources> resources = new HashSet<>(resourcesList);
        role.setResources(resources);
        roleRepository.save(role);
    }

    /**
     * 更新超级管理员角色资源
     *
     * @param code
     * @param allResources
     */
    @Override
    public void updateSuperAdminResources(String code, List<Resources> allResources) {
        Role role = roleRepository.findByRoleCode(code);
        Set<Resources> resources = role.getResources();
        resources.clear();
        resources.addAll(allResources);
        roleRepository.save(role);
    }


    private RoleVO toVO(Role role) {
        return new RoleVO(role);
    }


}
